package com.shangs.platform.modular.cms.catalog.controller;

import com.shangs.platform.common.constant.factory.ConstantFactory;
import com.shangs.platform.common.exception.BizExceptionEnum;
import com.shangs.platform.common.exception.BussinessException;
import com.shangs.platform.common.persistence.dao.CmsCatalogMapper;
import com.shangs.platform.common.persistence.model.CmsCatalog;
import com.shangs.platform.core.base.controller.BaseController;
import com.shangs.platform.core.log.LogObjectHolder;
import com.shangs.platform.core.node.ZTreeNode;
import com.shangs.platform.core.shiro.ShiroKit;
import com.shangs.platform.core.support.BeanKit;
import com.shangs.platform.core.util.ToolUtil;
import com.shangs.platform.modular.cms.warpper.CatalogWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 栏目控制器
 *
 * @author shangs
 * @Date 2017-10-25 15:22:09
 */
@Controller
@RequestMapping("/catalog")
public class CatalogController extends BaseController {

    private String PREFIX = "/cms/catalog/";

    @Resource
    CmsCatalogMapper cmsCatalogMapper;

    /**
     * 跳转到栏目首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "catalog.html";
    }

    /**
     * 跳转到添加栏目
     */
    @RequestMapping("/catalog_add")
    public String catalogAdd() {
        return PREFIX + "catalog_add.html";
    }

    /**
     * 跳转到修改栏目
     */
    @RequestMapping("/catalog_update/{catalogId}")
    public String catalogUpdate(@PathVariable Long catalogId, Model model) {
        if (ToolUtil.isEmpty(catalogId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsCatalog cmsCatalog = this.cmsCatalogMapper.selectByPrimaryKey(catalogId);

        Map<String, Object> map = BeanKit.beanToMap(cmsCatalog);
        if (cmsCatalog != null) {
            Long pId = cmsCatalog.getParentId();
            if (pId == null || pId == 0L) {
                map.put("pName", "顶级");
            } else {
                map.put("pName", ConstantFactory.me().getCatalogNameById(cmsCatalog.getParentId()));
            }
        }
        model.addAttribute("catalog", map);
        LogObjectHolder.me().set(map);
        return PREFIX + "catalog_edit.html";
    }

    /**
     * 获取栏目列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list = this.cmsCatalogMapper.selectByCondition(new CmsCatalog());
        return new CatalogWarpper(list).warp();
    }

    /**
     * 新增栏目
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsCatalog cmsCatalog) {
        cmsCatalog.setCreateId(ShiroKit.getUser().getId());
        cmsCatalog.setCreateTime(new Date());
        this.cmsCatalogMapper.insertSelective(cmsCatalog);
        return SUCCESS_TIP;
    }

    /**
     * 删除栏目
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long catalogId) {
        this.cmsCatalogMapper.deleteByPrimaryKey(catalogId);
        return SUCCESS_TIP;
    }


    /**
     * 修改栏目
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@Valid CmsCatalog cmsCatalog, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        cmsCatalog.setUpdateId(ShiroKit.getUser().getId());
        cmsCatalog.setUpdateTime(new Date());
        this.cmsCatalogMapper.updateByPrimaryKeySelective(cmsCatalog);
        return SUCCESS_TIP;
    }

    /**
     * 栏目详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam Long catalogId) {
        if (ToolUtil.isEmpty(catalogId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CmsCatalog cmsCatalog = this.cmsCatalogMapper.selectByPrimaryKey(catalogId);
        Map<String, Object> map = BeanKit.beanToMap(cmsCatalog);
        return map;
    }

    /**
     * 获取菜单列表(选择父级菜单用)
     */
    @RequestMapping(value = "/selectCatalogTreeList")
    @ResponseBody
    public List<ZTreeNode> selectCatalogTreeList() {
        List<ZTreeNode> roleTreeList = this.cmsCatalogMapper.catalogTreeList();
        roleTreeList.add(ZTreeNode.createParent());
        return roleTreeList;
    }

    /**
     * 获取部门的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.cmsCatalogMapper.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }
}
